package com.zz.nondatain.view.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zz.nondatain.R;
import com.zz.nondatain.base.BaseActivity;
import com.zz.nondatain.databinding.ActivityMessageBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息页面，显示系统消息列表和实时对话
 */
public class MessageActivity extends BaseActivity<ActivityMessageBinding> {

    private List<MessageItem> messages = new ArrayList<>();
    private MessageAdapter messageAdapter;
    
    private List<ChatRoomItem> chatRooms = new ArrayList<>();
    private ChatRoomAdapter chatRoomAdapter;

    // 当前选中的选项卡
    private static final int TAB_MESSAGE = 0;
    private static final int TAB_CHAT = 1;
    private int currentTab = TAB_MESSAGE;

    /**
     * 启动消息页面
     *
     * @param context 上下文
     */
    public static void start(Context context) {
        Intent intent = new Intent(context, MessageActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        // 设置消息RecyclerView
        binding.recyclerMessages.setLayoutManager(new LinearLayoutManager(this));
        messageAdapter = new MessageAdapter(messages);
        binding.recyclerMessages.setAdapter(messageAdapter);
        
        // 设置聊天室RecyclerView
        binding.recyclerChats.setLayoutManager(new LinearLayoutManager(this));
        chatRoomAdapter = new ChatRoomAdapter(chatRooms);
        binding.recyclerChats.setAdapter(chatRoomAdapter);
        
        // 默认显示消息tab
        showMessageTab();
        
        // 加载数据
        loadMessages();
        loadChatRooms();
    }

    @Override
    protected void initData() {
        // 初始化数据在load方法中完成
    }

    @Override
    protected void initObservers() {
        setupListeners();
    }

    @Override
    protected ActivityMessageBinding getViewBinding() {
        return ActivityMessageBinding.inflate(getLayoutInflater());
    }
    
    /**
     * 设置监听器
     */
    private void setupListeners() {
        // 返回按钮点击事件
        binding.btnBack.setOnClickListener(v -> finish());
        
        // 消息选项卡点击事件
        binding.tabMessage.setOnClickListener(v -> {
            if (currentTab != TAB_MESSAGE) {
                showMessageTab();
            }
        });
        
        // 实时对话选项卡点击事件
        binding.tabChat.setOnClickListener(v -> {
            if (currentTab != TAB_CHAT) {
                showChatTab();
            }
        });

    }
    
    /**
     * 显示消息选项卡
     */
    private void showMessageTab() {
        currentTab = TAB_MESSAGE;
        
        // 更新UI状态
        binding.tvTabMessage.setTextColor(getResources().getColor(android.R.color.holo_blue_light));
        binding.indicatorMessage.setVisibility(View.VISIBLE);
        binding.tvTabChat.setTextColor(getResources().getColor(android.R.color.darker_gray));
        binding.indicatorChat.setVisibility(View.INVISIBLE);
        
        // 切换显示内容
        binding.recyclerMessages.setVisibility(View.VISIBLE);
        binding.recyclerChats.setVisibility(View.GONE);
    }
    
    /**
     * 显示聊天选项卡
     */
    private void showChatTab() {
        currentTab = TAB_CHAT;
        
        // 更新UI状态
        binding.tvTabMessage.setTextColor(getResources().getColor(android.R.color.darker_gray));
        binding.indicatorMessage.setVisibility(View.INVISIBLE);
        binding.tvTabChat.setTextColor(getResources().getColor(android.R.color.holo_blue_light));
        binding.indicatorChat.setVisibility(View.VISIBLE);
        
        // 切换显示内容
        binding.recyclerMessages.setVisibility(View.GONE);
        binding.recyclerChats.setVisibility(View.VISIBLE);
    }
    
    /**
     * 加载消息数据
     */
    private void loadMessages() {
        // 清空现有记录
        messages.clear();
        
        // 添加模拟数据 - 实际应该从API或数据库获取
        messages.add(new MessageItem(
                MessageItem.TYPE_SYSTEM,
                "系统通知", 
                "测试消息通知",
                "2022-12-29"));
                
        messages.add(new MessageItem(
                MessageItem.TYPE_ANNOUNCEMENT,
                "通知公告", 
                "123123123",
                "2023-08-08"));
        
        // 刷新适配器
        messageAdapter.notifyDataSetChanged();
    }
    
    /**
     * 加载聊天室数据
     */
    private void loadChatRooms() {
        // 清空现有记录
        chatRooms.clear();
        
        // 添加模拟数据 - 实际应该从API或数据库获取
        chatRooms.add(new ChatRoomItem(
                "社区工作组",
                "请大家注意签到时间变更",
                "12:30"));
                
        chatRooms.add(new ChatRoomItem(
                "管理员群组",
                "明天有新的工作安排",
                "昨天"));
        
        // 刷新适配器
        chatRoomAdapter.notifyDataSetChanged();
    }

    /**
     * 过滤消息数据
     */
    private void filterMessages(String query) {
        // 实现过滤逻辑
    }

    /**
     * 消息数据类
     */
    private static class MessageItem {
        public static final int TYPE_SYSTEM = 1;
        public static final int TYPE_ANNOUNCEMENT = 2;
        
        int type;
        String title;
        String content;
        String time;
        
        MessageItem(int type, String title, String content, String time) {
            this.type = type;
            this.title = title;
            this.content = content;
            this.time = time;
        }
    }
    
    /**
     * 聊天室数据类
     */
    private static class ChatRoomItem {
        String name;
        String lastMessage;
        String time;
        
        ChatRoomItem(String name, String lastMessage, String time) {
            this.name = name;
            this.lastMessage = lastMessage;
            this.time = time;
        }
    }
    
    /**
     * 消息适配器
     */
    private class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {
        
        private List<MessageItem> messages;
        
        MessageAdapter(List<MessageItem> messages) {
            this.messages = messages;
        }
        
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_message, parent, false);
            return new ViewHolder(view);
        }
        
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            MessageItem message = messages.get(position);
            holder.tvTitle.setText(message.title);
            holder.tvContent.setText(message.content);
            holder.tvTime.setText(message.time);
            
            // 根据类型设置不同的图标和背景色
            if (message.type == MessageItem.TYPE_SYSTEM) {
                holder.ivIcon.setImageResource(android.R.drawable.ic_dialog_info);
                holder.ivIcon.setBackgroundResource(R.drawable.circle_background);
            } else if (message.type == MessageItem.TYPE_ANNOUNCEMENT) {
                holder.ivIcon.setImageResource(android.R.drawable.ic_dialog_alert);
                holder.ivIcon.setBackgroundResource(R.drawable.circle_background_red);
            }
            
            // 消息项点击事件
            holder.itemView.setOnClickListener(v -> {
                // 根据消息类型跳转到不同页面
                if (message.type == MessageItem.TYPE_ANNOUNCEMENT) {
                    // 跳转到公告详情页面
                    Intent intent = new Intent(MessageActivity.this, AnnouncementActivity.class);
                    startActivity(intent);
                } else {
                    // 显示系统消息详情 (简化实现为显示Toast)
                    showToast(message.content);
                }
            });
        }
        
        @Override
        public int getItemCount() {
            return messages.size();
        }
        
        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvTitle;
            TextView tvContent;
            TextView tvTime;
            ImageView ivIcon;
            
            ViewHolder(View itemView) {
                super(itemView);
                tvTitle = itemView.findViewById(R.id.tv_message_title);
                tvContent = itemView.findViewById(R.id.tv_message_content);
                tvTime = itemView.findViewById(R.id.tv_message_time);
                ivIcon = itemView.findViewById(R.id.iv_message_icon);
            }
        }
    }
    
    /**
     * 聊天室适配器
     */
    private class ChatRoomAdapter extends RecyclerView.Adapter<ChatRoomAdapter.ViewHolder> {
        
        private List<ChatRoomItem> chatRooms;
        
        ChatRoomAdapter(List<ChatRoomItem> chatRooms) {
            this.chatRooms = chatRooms;
        }
        
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_chat_room, parent, false);
            return new ViewHolder(view);
        }
        
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            ChatRoomItem chatRoom = chatRooms.get(position);
            holder.tvName.setText(chatRoom.name);
            holder.tvLastMessage.setText(chatRoom.lastMessage);
            holder.tvTime.setText(chatRoom.time);
            
            // 聊天室点击事件
            holder.itemView.setOnClickListener(v -> {
                // 跳转到聊天室 (简化实现为显示Toast)
                showToast("进入聊天室: " + chatRoom.name);
            });
        }
        
        @Override
        public int getItemCount() {
            return chatRooms.size();
        }
        
        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvName;
            TextView tvLastMessage;
            TextView tvTime;
            
            ViewHolder(View itemView) {
                super(itemView);
                tvName = itemView.findViewById(R.id.tv_chat_name);
                tvLastMessage = itemView.findViewById(R.id.tv_chat_last_message);
                tvTime = itemView.findViewById(R.id.tv_chat_time);
            }
        }
    }
} 